import SwiftUI
import SPGlassKit

public struct GlassTextFieldExample: GlassExample {
    public static let componentID = "glass-text-field"
    @State private var name = ""
    @State private var email = ""
    @State private var handle = "taken"
    @State private var locked = "Read-only value"
    @FocusState private var emailFocused: Bool

    public init() {}

    public var body: some View {
        ScrollView {
            VStack(alignment: .leading, spacing: 20) {
                GlassTextField(
                    "Full name",
                    text: $name,
                    prompt: "Ada Lovelace",
                    validation: GlassFieldValidation(.required(), .minLength(2)),
                    input: .name
                )
                .onSubmit { emailFocused = true }

                GlassTextField(
                    "Email",
                    text: $email,
                    prompt: "name@example.com",
                    helper: "Used only for the receipt.",
                    validation: GlassFieldValidation(.required(), .custom(message: "Enter an address such as name@example.com") { $0.contains("@") && $0.contains(".") }),
                    input: .email,
                    focus: $emailFocused
                )

                GlassTextField(
                    "Username",
                    text: $handle,
                    validation: GlassFieldValidation(.maxLength(12)),
                    validateOn: .always,
                    errorMessage: handle == "taken" ? "This username is already taken." : nil,
                    input: .username
                )

                GlassTextField("Account", text: $locked)
                    .disabled(true)

                Button("Focus the email field") { emailFocused = true }
            }
            .padding()
        }
    }
}

#Preview("GlassTextField") { GlassTextFieldExample().glassTheme(.aurora) }
